package fy.form.oa.dao;

import java.util.List;

import fy.uap.manager.InitJSP;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import fy.form.listen.bean.Node;
import fy.form.listen.set.OA_UAPAccountContrast;


public class OAOrgAccountDao {
	private static Log log = LogFactory.getLog(OAOrgAccountDao.class);

	private static String treeXML;

	public static String findAccountTree() {
		treeXML = null;
		if (treeXML == null) {
			try {
				// OrgManagerDirect orgManagerDirect = (OrgManagerDirect)
				// ApplicationContextHolder
				// .getBean("OrgManagerDirect");
				//
				// V3xOrgAccount root = orgManagerDirect
				// .getAccountById(-1730833917365171641L);

				Document doc = org.dom4j.DocumentHelper.createDocument();

				Element treeRoot = doc.addElement("tree");
				treeRoot.addAttribute("id", "0");
				treeRoot.addAttribute("open", "1");

				// Element child = treeRoot.addElement("item");
				//
				// child.addAttribute("id", String.valueOf(root.getId()));
				//
				// child.addAttribute("text", root.getName());
				//
				// List<V3xOrgAccount> childrenAccounts =
				// root.getChildrenAccounts();
				//
				// int size = childrenAccounts.size();
				//
				// Node[] nodes = new Node[size];
				//
				// for (int i = 0; i < size; i++) {
				// V3xOrgAccount voa = childrenAccounts.get(i);
				// Node node = new Node();
				// node.setId(voa.getId());
				// node.setText(voa.getName());
				// node.setSortID(voa.getSortId());
				// nodes[i] = node;
				// }
				// for (int i = 0; i < size; i++) {
				// for (int j = i + 1; j < size; j++) {
				// if (nodes[i].getSortID() > nodes[j].getSortID()) {
				// Node temp = nodes[i];
				// nodes[i] = nodes[j];
				// nodes[j] = temp;
				// temp = null;
				// }
				// }
				// }
				List<Node> nodes = OA_UAPAccountContrast.getOaAccountList();

				for (int i = 0, size = nodes.size(); i < size; i++) {
					Element item = treeRoot.addElement("item");
					log.info("id=>>>>>>>>>"+String.valueOf(nodes.get(i).getId()));
					item.addAttribute("id",
							String.valueOf(nodes.get(i).getId()));
					item.addAttribute("text", nodes.get(i).getText());
				}
				nodes = null;

				treeXML = treeRoot.asXML();

				doc.clearContent();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return treeXML;
	}

}
